package org.example.traffic.trainplan;

import org.example.traffic.organizationadmin.OrganizationAdmin;
import org.example.traffic.trainplan.TrainPlan;
import org.example.traffic.trainplan.TrainPlanRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class TrainPlanService {
    @Autowired
    private TrainPlanRepository trainPlanRepository;

    // 获取所有培训计划
    public List<TrainPlan> getAllTrainPlans() {
        return trainPlanRepository.findAll();
    }

    // 根据ID获取培训计划
    public Optional<TrainPlan> getTrainPlanById(Integer id) {
        return trainPlanRepository.findById(id);
    }

    // 创建培训计划
    public TrainPlan createTrainPlan(TrainPlan trainPlan) {
        return trainPlanRepository.save(trainPlan);
    }

    // 更新培训计划
    public TrainPlan updateTrainPlan(Integer id, TrainPlan trainPlanDetails) {
        TrainPlan trainPlan = trainPlanRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("TrainPlan not found with id: " + id));

        trainPlan.setTitle(trainPlanDetails.getTitle());
        trainPlan.setType(trainPlanDetails.getType());
        trainPlan.setStartDate(trainPlanDetails.getStartDate());
        trainPlan.setEndDate(trainPlanDetails.getEndDate());

        return trainPlanRepository.save(trainPlan);
    }


    // 分页查询培训计划
    public Page<TrainPlan> getTrainPlans(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        return trainPlanRepository.findAll(pageable);
    }
    // 删除培训计划
    public void deleteTrainPlan(Integer id) {
        trainPlanRepository.deleteById(id);
    }

}
